package SunStarView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/6.
 */

public abstract class LazyFragment extends Fragment {
    public View thisView;
    //可见性控制
    private boolean isVisible;
    //首次加载
    public boolean isFirstLoad;
    //控制是否准备成功，防止出现空指针
    private boolean isPrepare;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        thisView = inflater.inflate(initViewid(),null);
        isPrepare = true;
        isFirstLoad = true;
        initViewCompelte();
        loadData();
        return thisView;
    }

    private void loadData() {
        if(isPrepare&&isFirstLoad&&isVisible){
            initData();
            isFirstLoad = false;
        }else{
            reLook();
        }
    }
    public  void reLook(){};
    public abstract void initData();

    public abstract int initViewid();
    public abstract void initViewCompelte();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint()){//可见
            isVisible = true;
            loadData();
        }else{//不可见

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
}
